package config

import (
	"os"

	"gopkg.in/yaml.v2"
)

type AppConfig struct {
	App struct {
		Port         string `yaml:"port"`
		WorksPerSize int    `yaml:"works_per_size"`
		BusinessAddr string `yaml:"business_addr"`
		LogicAddr    string `yaml:"logic_addr"`
		Domain       string `yaml:"domain"`
		Prefix       string `yaml:"prefix"`
		Debug        bool   `yaml:"debug"`
		Mode         string `yaml:"mode"`
	} `yaml:"app"`

	Mysql struct {
		UserName string `yaml:"username"`
		PassWord string `yaml:"password"`
		Host     string `yaml:"host"`
		Port     int    `yaml:"port"`
		TimeOut  string `yaml:"timeout"`
		DBName   string `yaml:"db_name"`
	} `yaml:"mysql"`

	Log struct {
		File       string `yaml:"file"`
		MaxSize    int    `yaml:"max_size"`
		MaxBackups int    `yaml:"max_backups"`
		MaxAge     int    `yaml:"max_age"`
		Compress   bool   `yaml:"compress"`
	} `yaml:"log"`

	AliMsg struct {
		AccesskeyId     string `yaml:"accesskey_id"`
		AccesskeySecret string `yaml:"accesskey_secret"`
		TemplateCode    string `yaml:"template_code"`
		SignName        string `yaml:"sign_name"`
	} `yaml:"aliMsg"`

	Redis struct {
		Host     string `yaml:"host"`
		Port     int    `yaml:"port"`
		Password string `yaml:"password"`
		Db       int    `yaml:"db"`
	} `yaml:"redis"`

	Elasticsearch struct {
		Host       string `yaml:"host"`
		Port       int    `yaml:"port"`
		UserName   string `yaml:"username"`
		Password   string `yaml:"password"`
		Index      string `yaml:"index"`
		LabelIndex string `yaml:"label_index"`
	} `yaml:"elasticsearch"`

	Services struct {
		ConvSession              string `yaml:"conv_session"`
		NextQuestion             string `yaml:"next_question"`
		QScore                   string `yaml:"q_score"`
		LogicScore               string `yaml:"logic_score"`
		EvaluateScore            string `yaml:"evaluate_score"`
		FocusThemeScore          string `yaml:"focus_theme_score"`
		MoodURL                  string `yaml:"mood_url"`
		EmotionUrl               string `yaml:"emotion_url"`
		CharacterUrl             string `yaml:"character_url"`
		PicBeauty                string `yaml:"pic_beauty_url"`
		PartLabelsCache          string `yaml:"part_labels"`
		AlgRecommendNextQuestion string `yaml:"alg_recommend_next_question"`
		AlgRecommendFollowingAsk string `yaml:"alg_recommend_following_ask"`
		DataMask                 string `yaml:"data_mask"`
		EvalByLabelsZhuanxiang   string `yaml:"eval_by_labels_zhuanxiang"`
		AskGlm                   string `yaml:"ask_glm"`
		GetGlmResult             string `yaml:"get_glm_result"`
		CareerRecommend          string `yaml:"career_recommend"`
		GetGlm2GuideResult       string `yaml:"glm2_guide"`
		CustomLabelAdd           string `yaml:"custom_label_add"`
		CustomLabelGet           string `yaml:"custom_label_get"`
		CustomLabelPush          string `yaml:"custom_label_push"`
	} `yaml:"services"`

	MailInfo struct {
		AuthUrl          string `yaml:"auth_url"`
		AuthPort         int    `yaml:"auth_port"`
		BusinessMail     string `yaml:"business_mail"`
		BusinessPassword string `yaml:"business_password"`
	} `yaml:"mailInfo"`

	UfsInfo struct {
		BucketPrefix string `yaml:"bucket_prefix"`
		UfsUrl       string `yaml:"ufs_url"`
	} `yaml:"ufsInfo"`

	Wx struct {
		Appid           string `yaml:"appid"`
		Secret          string `yaml:"secret"`
		TokenUrl        string `yaml:"token_url"`
		TicketUrl       string `yaml:"ticket_url"`
		UserPhoneNumber string `yaml:"user_phone_number"`
		GenerateSchema  string `yaml:"generate_scheme"`
	} `yaml:"wx"`

	WxPa struct {
		Appid  string `yaml:"appid"`
		Secret string `yaml:"secret"`
	} `yaml:"wxpa"`

	Yz struct {
		ItemSearch          string `yaml:"item_search"`
		SecretDecryptSingle string `yaml:"secret_decrypt_single"`
		TokenUrl            string `yaml:"token_url"`
		ClientId            string `yaml:"client_id"`
		ClientSecret        string `yaml:"client_secret"`
		AuthorizeType       string `yaml:"authorize_type"`
		GrantId             string `yaml:"grant_id"`
		Refresh             string `yaml:"refresh"`
	} `yaml:"yz"`

	Yf struct {
		Url         string `yaml:"url"`
		TopicYoushi string `yaml:"topic_youshi"`
	} `yaml:"yf"`

	WS struct {
		AppID  string `yaml:"app_id"`
		AppKey string `yaml:"app_key"`
	} `yaml:"xfws"`

	Ex struct {
		Bd struct {
			GrantType string `yaml:"grant_type"`
			ClientId  string `yaml:"client_id"`
			AppSecret string `yaml:"app_secret"`
		} `yaml:"bd"`
		Hb struct {
			GrantType string `yaml:"grant_type"`
			ClientId  string `yaml:"client_id"`
			AppSecret string `yaml:"app_secret"`
		} `yaml:"hb"`
		Rd struct {
			GrantType string `yaml:"grant_type"`
			ClientId  string `yaml:"client_id"`
			AppSecret string `yaml:"app_secret"`
		} `yaml:"rd"`
		Bh struct {
			GrantType string `yaml:"grant_type"`
			ClientId  string `yaml:"client_id"`
			AppSecret string `yaml:"app_secret"`
		} `yaml:"bh"`
		Bl struct {
			GrantType string `yaml:"grant_type"`
			ClientId  string `yaml:"client_id"`
			AppSecret string `yaml:"app_secret"`
		} `yaml:"bl"`
		Zpqy struct {
			GrantType string `yaml:"grant_type"`
			ClientId  string `yaml:"client_id"`
			AppSecret string `yaml:"app_secret"`
		} `yaml:"zpqy"`
	} `yaml:"ex"`

	Beida struct {
		Url   string `yaml:"url"`
		Token string `yaml:"token"`
	} `yaml:"beida"`
	// 声网第三方功能模块
	ShengWang struct {
		Appid          string `yaml:"appid"`
		CustomerKey    string `yaml:"customerKey"`
		CustomerSecret string `yaml:"customerSecret"`
		AppCertificate string `yaml:"appCertificate"`
		CommonUrl      string `yaml:"commonUrl"`
	} `yaml:"shengwang"`
	// 阿里云oss
	AliOss struct {
		AliBucket    string `yaml:"aliBucket"`
		AliAccessKey string `yaml:"aliAccessKey"`
		AliSecretKey string `yaml:"aliSecretKey"`
		OssUrl       string `yaml:"ossUrl"`
	} `yaml:"alioss"`
}

func InitConfig(configPath string) (*AppConfig, error) {
	conf := &AppConfig{}
	f, err := os.Open(configPath)
	if err != nil {
		return nil, err
	}

	err = yaml.NewDecoder(f).Decode(conf)
	if err != nil {
		return nil, err
	}

	return conf, nil
}
